Arrange multiple plots into a grid

There are two avalaible options to arrange ggiraph outputs into a grid. As ggiraph is mainly only new geoms, package cowplot and patchwork can be used seamlessly.

Using cowplot

Mouseover points to see where they are located in the other graph.

girafe( code = print(gg1 + gg2), width_svg = 8, height_svg = 4)

Using patchwork

Mouseover points to see where they are located in the other graph.

girafe( code = print(gg1 + gg2), width_svg = 8, height_svg = 4)

Dynamic dendrogram

expr.R

##         col_4    col_5    col_6    col_7    col_8    col_9
## id_1 3.943339 3.885332 3.972568 3.897985 3.968078 3.840585
## id_2 2.405950 2.364125 3.035228 2.581442 2.476972 2.755192
## id_3 1.845634 1.876945 2.006225 1.917262 2.001037 1.979254
## id_4 3.032080 2.923366 3.011424 3.049322 3.158107 3.008166

First compute data for dendrograms

Create a data.frame from the matrix, then order.

Add a variable to have tooltips as tables

## # A tibble: 6 x 4
##   gene  variable measure tooltip                                          
##   <fct> <fct>      <dbl> <chr>                                            
## 1 id_8  col_22      4.21 <table><tr><td>gene</td><td>id_8</td></tr><tr><t…
## 2 id_8  col_20      3.87 <table><tr><td>gene</td><td>id_8</td></tr><tr><t…
## 3 id_8  col_25      3.87 <table><tr><td>gene</td><td>id_8</td></tr><tr><t…
## 4 id_8  col_44      3.27 <table><tr><td>gene</td><td>id_8</td></tr><tr><t…
## 5 id_8  col_45      3.72 <table><tr><td>gene</td><td>id_8</td></tr><tr><t…
## 6 id_8  col_33      3.38 <table><tr><td>gene</td><td>id_8</td></tr><tr><t…

Create the ggplot using geom_tile_interactive

p <- ggplot(data = expr_set, aes(x = variable, y = gene) ) +
  geom_tile_interactive(aes(fill = measure, tooltip = tooltip), colour = "white") +
  scale_fill_gradient(low = "white", high = "#BC120A", limits = c(0, 13)) +
  geom_segment(
    data = data_c,
    mapping = aes(x = x, y = yend, xend = xend, yend = y),
    colour = "gray20", size = .2) +
  geom_segment(
    data = data_r,
    mapping = aes(x = x_, y = y_, xend = xend_, yend = yend_),
    colour = "gray20", size = .2) +
  coord_equal()

Theme the object

p <- p + theme_minimal() +
  theme(
    legend.position = "right",
    panel.grid.minor = element_line(color = "transparent"),
    panel.grid.major = element_line(color = "transparent"),
    axis.ticks.length   = unit(2, units = "mm"),
    plot.title = element_text(face = "bold", hjust = 0.5, size = 12),
    axis.title = element_text(size = 9, colour = "gray30"),
    axis.text.y = element_text(hjust = 1, size = 5, colour = "gray40"),
    axis.text.x = element_text(angle = 90, hjust = 1, size = 5, colour = "gray40"),
    legend.title=element_text(face = "bold", hjust = 0.5, size=8),
    legend.text=element_text(size=6)
  )

Use girafe

girafe(ggobj = p, width_svg = 8, height_svg = 6)

Network with ggraph

The following code is adapted from http://www.pieceofk.fr/?p=431.

library(tidyverse)
library(stringr)
library(igraph)
library(tidygraph)
library(ggraph)
library(magrittr)
library(ggiraph)

pdb <- tools::CRAN_package_db()

aut <- pdb$Author %>%
  str_replace_all("\\(([^)]+)\\)", "") %>%
  str_replace_all("\\[([^]]+)\\]", "") %>%
  str_replace_all("<([^>]+)>", "") %>%
  str_replace_all("\n", " ") %>%
  str_replace_all("[Cc]ontribution.* from|[Cc]ontribution.* by|[Cc]ontributors", " ") %>%
  str_replace_all("\\(|\\)|\\[|\\]", " ") %>%
  iconv(to = "ASCII//TRANSLIT") %>%
  str_replace_all("'$|^'", "") %>%
  gsub("([A-Z])([A-Z]{1,})", "\\1\\L\\2", ., perl = TRUE) %>%
  gsub("\\b([A-Z]{1}) \\b", "\\1\\. ", .) 

aut <- aut %>%
  purrr::map(str_split, ",|;|&| \\. |--|(?<=[a-z])\\.| [Aa]nd | [Ww]ith | [Bb]y ", simplify = TRUE) %>%
  purrr::map(str_replace_all, "[[:space:]]+", " ") %>%
  purrr::map(str_replace_all, " $|^ | \\.", "") %>%
  purrr::map(function(x) x[str_length(x) != 0]) %>%
  set_names(pdb$Package) %>%
  extract(map_lgl(., function(x) length(x) > 1))

aut_list <- aut %>%
  unlist() %>%
  dplyr::as_data_frame() %>%
  count(value) %>%
  rename(Name = value, Package = n) %>% 
  mutate(Name = str_replace_all(Name, "'", " ")) 


edge_list <- aut %>%
  purrr::map(combn, m = 2) %>%
  do.call("cbind", .) %>%
  t() %>%
  dplyr::as_data_frame() %>%
  arrange(V1, V2) %>%
  count(V1, V2)

g <- edge_list %>%
  select(V1, V2) %>%
  as.matrix() %>%
  graph.edgelist(directed = FALSE) %>%
  as_tbl_graph() %>%
  activate("edges") %>%
  mutate(Weight = edge_list$n) %>%
  activate("nodes") %>%
  rename(Name = name) %>%
  mutate(Component = group_components()) %>%
  filter(Component == names(table(Component))[which.max(table(Component))])


g <- g %>%
  left_join(aut_list) %>%
  filter(Package > 4) %>%
  mutate(Component = group_components()) %>%
  filter(Component == names(table(Component))[which.max(table(Component))])


g <- mutate(g, Community = group_edge_betweenness(),
            Degree = centrality_degree())


g <- g %>%
  mutate(Community = case_when(Community == names(sort(table(Community),
                                                       decr = TRUE))[1] ~ "The Ancients",
                               Community == names(sort(table(Community),
                                                       decr = TRUE))[2] ~ "The Moderns",
                               Community %in% names(sort(table(Community),
                                                         decr = TRUE))[-1:-2] ~ "Unclassified")) %>%
  mutate(Community = factor(Community))

g <- g %>%
  filter(Degree > 5) %>%
  mutate(Degree = centrality_degree())

We can now create the plot :

ggg <- ggraph(g, layout = 'lgl') +
  geom_edge_fan(alpha = 0.1, edge_width = .2) +
  geom_point_interactive(aes(x, y,
                             tooltip = Name, data_id = Name, color = Community,
                             size = Package), alpha = .7 ) +
  theme_graph() +
  scale_color_manual(values=c("The Ancients" = "#5BC8AC", "The Moderns" = "#F18D9E", Unclassified = "#E6D72A")) +
  theme(legend.position = "bottom")

girafe(ggobj = ggg, width_svg = 8, height_svg = 6)